/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.EstablishmentBean;
import com.minierp.bean.EstablishmentContactBean;
import com.minierp.dao.EstablishmentDao;
import com.minierp.helper.PageHelper;
import com.minierp.model.Establishment;
import com.minierp.model.EstablishmentContact;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class EstablishmentValidator implements Validator {
	private EstablishmentDao establishmentDao;

	public boolean supports(Class clazz) {
		return EstablishmentBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		EstablishmentBean bean = (EstablishmentBean) obj;
		Establishment form = bean.getEstablishment();
		boolean hasError = false;
		Establishment i;
		if (bean.getCodeRule().getGenerateRule().equals("MANUAL")
				|| form.getId() != 0L) {
			if (form.getEstCode() != null
					&& !form.getEstCode().trim().equals("")) {
				i = this.establishmentDao.getByEstCode(form.getEstCode(),
						form.getEstType());
				if (i != null && i.getId() != form.getId()) {
					errors.rejectValue("establishment.estCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("establishment.estCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			if (form.getEstName() != null) {
				form.setEstName(form.getEstName().trim());
			}

			if (form.getEstName() != null && !form.getEstName().equals("")) {
				if (form.getTallySetId() == 0L) {
					errors.rejectValue("establishment.tallySetId", "",
							"请选择『当前开单帐套』");
					hasError = true;
				} else {
					i = this.establishmentDao.getByEstName(form.getEstName(),
							form.getEstType());
					if (i != null && i.getId() != form.getId()
							&& i.getTallySetId() == form.getTallySetId()) {
						if (i.getEstType().equals("CLIENT")) {
							errors.rejectValue("establishment.estName", "",
									"已经存在相同『名称』的客户");
						} else if (i.getEstType().equals("PROVIDER")) {
							errors.rejectValue("establishment.estName", "",
									"已经存在相同『名称』的供应商");
						} else if (i.getEstType().equals("COOPERATOR")) {
							errors.rejectValue("establishment.estName", "",
									"已经存在相同『名称』的外协商");
						} else {
							errors.rejectValue("establishment.estName", "",
									"已经存在相同『名称』的记录");
						}

						hasError = true;
					}
				}
			} else {
				errors.rejectValue("establishment.estName", "", "请输入『名称』");
				hasError = true;
			}

			if (!hasError) {
				if (!hasError && form.getSuperiorId() != 0L
						&& form.getId() != 0L) {
					if (form.getSuperiorId() == form.getId()) {
						hasError = true;
						errors.rejectValue("superiorName", "", "不能选择自己作为『上级』");
					} else {
						i = (Establishment) this.establishmentDao.get(
								Establishment.class, form.getSuperiorId());
						if (i != null
								&& i.getSuperiorIdStr() != null
								&& i.getSuperiorIdStr().indexOf(
										";" + form.getId() + ";") != -1) {
							hasError = true;
							errors.rejectValue("superiorName", "",
									"不能选择自己的下级作为『上级』");
						}
					}
				}

				if (!hasError) {
					if (form.getMark() != null
							&& form.getMark().length() > 2000) {
						hasError = true;
						errors.rejectValue("establishment.mark", "",
								"『备注』不能超过2000个字符");
					} else {
						for (int arg9 = 0; arg9 < bean.getContacts().size(); ++arg9) {
							EstablishmentContactBean cb = (EstablishmentContactBean) bean
									.getContacts().get(arg9);
							EstablishmentContact c = cb
									.getEstablishmentContact();
							if (cb.getStatus().equals("EDIT")
									|| cb.getStatus().equals("NEW")) {
								boolean hasInput = false;
								if (c.getContactName() != null
										&& !c.getContactName().equals("")) {
									hasInput = true;
								} else if (c.getEmail() != null
										&& !c.getEmail().equals("")) {
									hasInput = true;
								} else if (c.getIm() != null
										&& !c.getIm().equals("")) {
									hasInput = true;
								} else if (c.getMark() != null
										&& !c.getMark().equals("")) {
									hasInput = true;
								} else if (c.getMobile() != null
										&& !c.getMobile().equals("")) {
									hasInput = true;
								} else if (c.getPhone() != null
										&& !c.getPhone().equals("")) {
									hasInput = true;
								} else if (c.getPossie() != null
										&& !c.getPossie().equals("")) {
									hasInput = true;
								} else if (c.getSex() != null
										&& !c.getSex().equals("")) {
									hasInput = true;
								}

								if (!hasInput) {
									cb.setStatus("DELETE");
								} else {
									if (c.getContactName() == null
											|| c.getContactName().equals("")) {
										hasError = true;
										errors.rejectValue("contacts", "",
												"请输入『联系人姓名』");
										break;
									}

									if ((c.getMobile() == null || c.getMobile()
											.equals(""))
											&& (c.getPhone() == null || c
													.getPhone().equals(""))) {
										hasError = true;
										errors.rejectValue("contacts", "",
												"请输入『联系人手机』或『联系人电话』");
										break;
									}

									if (c.getEmail() != null
											&& !c.getEmail().equals("")
											&& !PageHelper
													.isEmail(c.getEmail())) {
										hasError = true;
										errors.rejectValue("contacts", "",
												"联系人『Email』格式不正确");
										break;
									}
								}
							}
						}
					}
				}
			}
		}

	}

	public EstablishmentDao getEstablishmentDao() {
		return this.establishmentDao;
	}

	public void setEstablishmentDao(EstablishmentDao establishmentDao) {
		this.establishmentDao = establishmentDao;
	}
}